#include <string.h>
#include "terminal.h"
#include "inout.h"
#include "mem-check.h"	
#include "control.h"
#include "avr/eeprom.h"
#include "uart.h"

/*
todo
0 bei zeitausgabe
ioname buffer output funktioniert nicht richtig
*/

void printstr(PGM_P addr);
void getiostr(uint8_t pos);

bool reprint;


const char m_instr[] PROGMEM = "  m";
const char h_instr[] PROGMEM = "  h";
const char s_instr[] PROGMEM = "  s";
const char free_instr[] PROGMEM = "        ";

void p_menu(void)
{		
		uint8_t i;
		uint8_t j;
		uint8_t posx = FRAMEPOS_X;
		uint8_t posy = FRAMEPOS_Y;
		uint8_t pos;
		set_cursorpos(posx, posy);



		for(i = 0; i < IOPORTS; i++) 					
		{	

			j = 0;
			if((selectedpos == j) && (subselectedpos == i))
			{
				setmode(MODE_REVERSED);
			}
						
			getiostr(i);
			pstring(ionamebuff);

			setmode(MODE_NONE);
		
			
			pos = posx + strlen(ionamebuff);
			while(pos <= (FRAMEPOS_X + 9))
			{	
				pchar(' ');
				pos++;
			}

			j++;
			if((selectedpos == j) && (subselectedpos == i))
			{
				setmode(MODE_REVERSED);
			}

			if(getstate(i))
			{
 				setcolour(COL_FOREGROUND, COL_GREEN);
				printstr(PSTR("ON "));	
			}
			else
			{	
				setcolour(COL_FOREGROUND, COL_RED);
				printstr(PSTR("OFF"));
			}

			

			setmode(MODE_NONE);
			setcolour(COL_FOREGROUND, COL_WHITE);

			set_cursorpos(posx, ++posy);
		
	}
}

void print_inputfield()
{	
	set_cursorpos(FRAMEPOS_X + selectedpos, FRAMEPOS_Y + subselectedpos);
	setmode(MODE_REVERSED);
	printstr(free_instr);
	set_cursorpos(FRAMEPOS_X + selectedpos, FRAMEPOS_Y + subselectedpos);
}


void getiostr(uint8_t pos)
{
	eeprom_read_block(ionamebuff, ionames[pos], sizeof(ionamebuff));
}


void cls()
{
	pstring(ESC);
	pstring("2J");
	set_cursorpos(1, 1);
}

void set_cursorpos(uint8_t posx, uint8_t posy)
{
	pstring(ESC);
	pdigit(posy);
	pchar(';');
	pdigit(posx);
	pchar('H');
}

void setcolour(uint8_t fgbg, uint8_t colour)
{
	pstring(ESC);
	pdigit(fgbg);
	pdigit(colour);
	pchar('m');
}

void setmode(uint8_t mode)
{
	pstring(ESC);
	pdigit(mode);
	pchar('m');
}

void printchar(PGM_P addr)
{
	pchar(pgm_read_byte(addr));
} 

void printstr(PGM_P addr)
{
	uint8_t c;
	while (c = pgm_read_byte (addr++), c != '\0')
	{
		pchar(c);
	}
}

void echo(uint8_t val)
{
	pchar(val);
}

static void (*send)(uint8_t) = &uart_send;

void pchar(uint8_t data)
{
	send(data);
}

#define MAX_NUM_DEC 10000

void pdigit(uint16_t data)
{
	uint16_t int_tmp = data;
	uint16_t i;

	if(data == 0)
	{
		send(48);
		return;
	}

	for(i=MAX_NUM_DEC; i; i/=10)
	{
		if(data/i)
		{
			send((int_tmp/i)+48);
			int_tmp %= i;
		}
	}
}

void phex(uint16_t data)
{
	uint16_t tmp;

	while(data != 0)
	{
		tmp = data & 0xF000;
		tmp = tmp >> 12;

		if(tmp < 10)
		{
			tmp = tmp + 48;
		}
		else
		{
			tmp = tmp + 55;
		}		
	
		send(tmp);
		data = data << 4;
	}
}

void pstring(char *string)
{
	while((*string) != '\0')
	{	
		send(*string);
		string++;
	}
}


